package src.com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class MaterialFacServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取android 客户端上传的数据
        //数据类型等判断根据header中的post_type判断
        System.out.println("Post 正在执行。。。。");

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        final String getPostType = request.getHeader("post_type");
        System.out.println("getposttype: " + getPostType);
        if (getPostType != null && getPostType.equals("feed_back_post")) {
            InputStream input = request.getInputStream();        //获取请求体的数据
            File file = null;
            file = new File(request.getServletContext().getRealPath("/") + "images\\bitmap.png");
            if (!file.exists()){
                System.out.println("File不存在");
                file.createNewFile();
            }
            if (file.exists()){
                System.out.println("File存在");
            }
            BufferedOutputStream bufferout = null;
            FileOutputStream out = new FileOutputStream(file);        //文件输出流
            byte[] data = new byte[10240];
            int len = 0;
            StringBuffer strbuf = new StringBuffer();
            bufferout = new BufferedOutputStream(out);
            while ((len = input.read(data)) != -1) {
                //strbuf.append((new String(data,0,len)));
                bufferout.write(data, 0, len);
                System.out.println("data存在不为NULL");
            }
            System.out.println("data is: " + new String(data));
            if (bufferout != null)
                bufferout.close();
            if (out != null)
                out.close();
            if (input != null)
                input.close();
        }
    }
}
